{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d36d18a2",
   "metadata": {},
   "source": [
    "# 梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "42fb0dd5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征字段： [[1 1 1]\n",
      " [1 2 1]\n",
      " [2 2 1]]\n",
      "目标字段： [1 2 3]\n",
      "初始权重： [0. 0. 0.]\n",
      "[0.52694572 0.62365992 0.30545245] ===> 0.12301912162580764\n",
      "[0.64708621 0.65870834 0.10248654] ===> 0.08431125702167834\n",
      "[ 0.7307205   0.68988934 -0.07126926] ===> 0.05883354536985593\n",
      "[ 0.79285084  0.72243781 -0.21687443] ===> 0.04139749153302678\n",
      "[ 0.8393243   0.75434858 -0.33910726] ===> 0.029304890590594018\n",
      "[ 0.87434276  0.78444952 -0.44187486] ===> 0.0208343711621309\n",
      "[ 0.90093491  0.81211972 -0.52838791] ===> 0.014857682242237177\n",
      "[ 0.9212914   0.83709618 -0.6012963 ] ===> 0.01061842603912178\n",
      "[ 0.93700304  0.85934194 -0.66279563] ===> 0.007600255925052549\n",
      "[ 0.94923025  0.87895708 -0.71471114] ===> 0.005445748870052196\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 使用梯度下降算法训练模型：Y = AX\n",
    "# 支持多个特征\n",
    "# points = [(1,1), (2,2), (4,2)]        # 样本数据: y = ax + b\n",
    "points = [(1,1,1), (1,2,2), (2,2,3)]  # 样本数据: y = ax1 + bx2 + c\n",
    "learning_rate = 0.01     # 学习率，注意对于[(1,1), (2,2), (4,2)]，如果学习率为0.1，将不再收敛\n",
    "epochs = 500             # 迭代次数\n",
    "\n",
    "def parse_points(points):\n",
    "    \"\"\"生成特征集合目标集\"\"\"\n",
    "    return np.array([np.append(row[:-1], 1) for row in points]), np.array([row[-1] for row in points])\n",
    "\n",
    "def init_w(n):\n",
    "    \"\"\"初始化权重\"\"\"\n",
    "    return np.zeros(n)\n",
    "\n",
    "def cal_loss(w):\n",
    "    \"\"\"计算损失\"\"\"\n",
    "    sum_loss = [(sum(w*x)-y)**2 for x, y in zip(train_X, train_Y)]\n",
    "    return sum(sum_loss) / len(points)\n",
    "\n",
    "def gradient(w, i):\n",
    "    \"\"\"计算损失函数在第i个权重上的梯度\"\"\"\n",
    "    loss_grad = [2*(sum(w*x) - y)*x[i] for x, y in zip(train_X, train_Y)]\n",
    "    grad = sum(loss_grad)\n",
    "    return grad\n",
    "\n",
    "def update(w):\n",
    "    \"\"\"更新权重\"\"\"\n",
    "    new_w = [wi - learning_rate * gradient(w, i) for i, wi in zip(range(len(w)), w)]\n",
    "    return np.array(new_w)\n",
    "\n",
    "train_X, train_Y = parse_points(points)\n",
    "print('特征字段：', train_X)\n",
    "print('目标字段：', train_Y)\n",
    "w = init_w(len(train_X[0]))\n",
    "print('初始权重：', w)\n",
    "\n",
    "for i in range(epochs):\n",
    "    w = update(w)\n",
    "    loss = cal_loss(w)\n",
    "    if i % 50 == 9:\n",
    "        print(w, \"===>\", loss)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
